import com.qian.api.ddj.model.PayFailed;
import com.qian.api.ddj.model.PayTypeDict;
import com.qian.api.ddj.model.StatusDict;
import com.qian.api.ddj.util.SignUtils;
import com.qian.ddj.pay.util.HttpClient;
import com.sun.tools.doclets.formats.html.markup.HtmlAttr;
import com.yaowk.api.utils.HttpUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.Test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class jdf {
   public static String key = "63652fec58ea76f66a9a4621d6b9ebbb";
   public static String HOST = "http://baidu.com";
    @Test
    public void aaa(){

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Map<String, String> map = new TreeMap();
        map.put("service", "pay.qq.wap");

        map.put("version", "1.0");
        map.put("merchantId", "180161");
        map.put("orderNo", String.valueOf(new Date().getTime()));
        map.put("tradeDate", sdf.format(date));
        map.put("tradeTime", "123015");
        map.put("amount", "100");
        map.put("clientIp", "127.0.0.1");
        map.put("notifyUrl", HOST+"/notify/jdfNotify");
        map.put("sign", com.qian.ddj.pay.util.SignUtils.createSign(map, key));
        String reqUrl = "https://paygw.pk10000.com/gateway";
        CloseableHttpResponse response = null;
        CloseableHttpClient client = null;
        String res = null;
        try {
            HttpPost httpPost = new HttpPost(reqUrl);
            List<NameValuePair> nvps = new ArrayList();
            for (String k : map.keySet()) {
                nvps.add(new BasicNameValuePair(k, map.get(k)));
            }
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
            httpPost.addHeader("Connection", "close");
            client = HttpClient.createHttpClient();
            response = client.execute(httpPost);
            System.out.println("==========");
            System.out.println(response);
            if (response != null && response.getEntity() != null) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                Map<String, String> resultMap = com.qian.ddj.pay.util.SignUtils.parseQuery(content);
                System.out.println(resultMap);
                String retUrl = resultMap.get("resultUrl");
                System.out.println(retUrl);
//                Document document =Jsoup.connect(retUrl).get();
//                document.html();
//                String html = document.html();
//                System.out.println(html);
//                Integer begin = html.indexOf("['script','[\"showQr(\\'");
//                Integer end = html.indexOf("\\')\"]']);");
//                System.out.println(begin+"      "+end);
//                html = html.substring(begin,end);
//                System.out.println("截取的字符串：\n"+html);
//                begin = html.indexOf("http");
//
//                html = html.substring(begin);
//                System.out.println("未转化的URL：\n"+html);
//                html = html.replaceAll("\\\\","");
//                System.out.println(html);
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
